//
//  TimerTableView.m
//  layerTest
//
//  Created by hcy on 14-3-23.
//  Copyright (c) 2014年 cy. All rights reserved.
//

#import "TimerTableView.h"

@implementation TimerTableView

- (id)initWithFrame:(CGRect)frame
{
    CGRect rect = frame;
    rect.size.height = kTimerTableViewCellHeight * 3;
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    CGRect rect = frame;
    rect.size.height = kTimerTableViewCellHeight * 3;
    
    [super setFrame:rect];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)start:(NSInteger)start
{
    NSLog(@"start:%d", start + self.count/2);
    NSIndexPath *startIndex = [NSIndexPath indexPathForRow:start + self.count/2 inSection:0];
    [self scrollToRowAtIndexPath:startIndex atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
    [self setTableMiddleCellHeightLight];
}

//调整为中间对齐
- (void)adjustTableCenterAlign
{
    NSArray *indexArray = [self indexPathsForVisibleRows];
    
    if ([indexArray count]>3) {
        
        NSIndexPath *index1 = [indexArray objectAtIndex:0];
        NSIndexPath *index4 = [indexArray objectAtIndex:3];
        
        CGRect rect1 = [self rectForRowAtIndexPath:index1];
        CGRect rect4 = [self rectForRowAtIndexPath:index4];
        
        CGFloat maxY1 = CGRectGetMaxY(rect1);
        
        //保留顶cell
        if ((maxY1 - self.contentOffset.y)>= kTimerTableViewCellHeight/2) {
            [self scrollRectToVisible:rect1 animated:YES];
        }
        else//不显示顶部cell
        {
            [self scrollRectToVisible:rect4 animated:YES];
        }
    }
    else//刚好在中间
    {
        NSLog(@"____3");
        
        [self setTableCircle];
    }
    
}

- (void)logRect:(CGRect)rect
{
    NSLog(@"x:%f, y:%f, dx:%f, dy:%f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}

//设置中间高亮
- (void)setTableMiddleCellHeightLight
{
    NSArray *indexArray = [self indexPathsForVisibleRows];
    
    if ([indexArray count]>= 3) {
        NSIndexPath *index1 = [indexArray objectAtIndex:0];
        NSIndexPath *index2 = [indexArray objectAtIndex:1];
        NSIndexPath *index3 = [indexArray objectAtIndex:2];
        //设置中间cell为高亮
        UITableViewCell *cellMid = [self cellForRowAtIndexPath:index2];
        [cellMid.textLabel setTextColor:kSelectColor];
        
        //上下为灰
        UITableViewCell *cell1 = [self cellForRowAtIndexPath:index1];
        UITableViewCell *cell3 = [self cellForRowAtIndexPath:index3];
        
        [cell1.textLabel setTextColor:kNotSelectColor];
        [cell3.textLabel setTextColor:kNotSelectColor];
    }
    
}


//调整为循环模式
- (void)setTableCircle
{
    NSArray *indexArray = [self indexPathsForVisibleRows];
    NSIndexPath *index1 = [indexArray objectAtIndex:0];
    NSIndexPath *index3 = [indexArray objectAtIndex:2];
    
    NSInteger count = self.count;
    
    if (index1.row < count/2) {//顶部调整至中间
        NSLog(@"___顶——调整至中间 %@", index1);
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index1.row + count inSection:index1.section];
        
        [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        
        [self setTableMiddleCellHeightLight];
        
        return;
    }
    
    if (index3.row > count + count/2) {//底部调整至中间
        NSLog(@"__底_调整至中间");
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index3.row - count inSection:index3.section];
        [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
        [self setTableMiddleCellHeightLight];
        return;
    }
    NSLog(@"__不_调整");
    [self setTableMiddleCellHeightLight];
    return;
}

- (NSInteger)currentIndex
{
    NSArray *indexArray = [self indexPathsForVisibleRows];
    
    NSIndexPath *index2 = [indexArray objectAtIndex:1];
    
    NSInteger index = (index2.row + self.count/2)%self.count;
    
    return index;
}

- (void)addOne
{
//    if ((self.currentIndex + 1) > self.count + self.count/2) {
//        NSIndexPath *startIndex = [NSIndexPath indexPathForRow:self.currentIndex + 1 - self.count inSection:0];
//        [self scrollToRowAtIndexPath:startIndex atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
//    }
    [self start:self.currentIndex + 1];
    
//    NSArray *indexArray = [self indexPathsForVisibleRows];
//    NSIndexPath *index1 = [indexArray objectAtIndex:1];
//    
//    NSIndexPath *startIndex = [NSIndexPath indexPathForRow:index1.row + 1 inSection:0];
//    [self scrollToRowAtIndexPath:startIndex atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}

- (void)cutOne
{
    [self start:self.currentIndex - 1];
    
//    if ((self.currentIndex - 1)<self.count/2) {
//        NSIndexPath *startIndex = [NSIndexPath indexPathForRow:self.currentIndex - 1 + self.count inSection:0];
//        [self scrollToRowAtIndexPath:startIndex atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
//    }
//    NSArray *indexArray = [self indexPathsForVisibleRows];
//    NSIndexPath *index1 = [indexArray objectAtIndex:1];
//    
//    NSIndexPath *startIndex = [NSIndexPath indexPathForRow:index1.row - 1 inSection:0];
//    [self scrollToRowAtIndexPath:startIndex atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}

@end
